<template>
    <div class="sidebar">
        <el-menu class="sidebar-el-menu" :default-active="onRoutes" :collapse="sidebar.collapse"
            background-color="#324157" text-color="#bfcbd9" active-text-color="#20a0ff" unique-opened router>
            <template v-for="item in items">
                <template v-if="item.subs">
                    <el-sub-menu :index="item.index" :key="item.index">
                        <template #title>
                            <el-icon>
                                <component :is="item.icon"></component>
                            </el-icon>
                            <span>{{ item.title }}</span>
                        </template>
                        <template v-for="subItem in item.subs">
                            <el-sub-menu v-if="subItem.subs" :index="subItem.index" :key="subItem.index">
                                <template #title>{{ subItem.title }}</template>
                                <el-menu-item v-for="(threeItem, i) in subItem.subs" :key="i" :index="threeItem.index">
                                    {{ threeItem.title }}
                                </el-menu-item>
                            </el-sub-menu>
                            <el-menu-item v-else :index="subItem.index">
                                {{ subItem.title }}
                            </el-menu-item>
                        </template>
                    </el-sub-menu>
                </template>
                <template v-else>
                    <el-menu-item :index="item.index" :key="item.index">
                        <el-icon>
                            <component :is="item.icon"></component>
                        </el-icon>
                        <template #title>{{ item.title }}</template>
                    </el-menu-item>
                </template>
            </template>
        </el-menu>
    </div>
</template>

<script setup>
import { computed } from "vue";
import { useSidebarStore } from "../store/sidebar";
import { useRoute } from "vue-router";

const items = [
    {
        icon: "Odometer",
        index: "/dashboard",
        title: "首页",
        permiss: "1",
    },
    {
        icon: "Calendar",
        index: "1",
        title: "用户管理",
        permiss: "2",
        subs: [
            {
                index: "/userlist",
                title: "用户列表",
                permiss: "2",
            },
            {
                index: "/user",
                title: "个人中心",
                permiss: "2",
            },
        ],
    },
    {
        icon: "Calendar",
        index: "1",
        title: "轮播图管理",
        permiss: "2",
        subs: [
            {
                index: "/bannerlist",
                title: "轮播图列表",
                permiss: "2",
            },
            {
                index: "/banner",
                title: "发布轮播图",
                permiss: "2",
            },
        ],
    },
    {
        icon: "Calendar",
        index: "1",
        title: "新闻管理",
        permiss: "2",
        subs: [
            {
                index: "/newsList",
                title: "新闻列表",
                permiss: "2",
            },
            {
                index: "/news",
                title: "发布新闻",
                permiss: "2",
            },
        ],
    },
];

const route = useRoute();
const onRoutes = computed(() => {
    return route.path;
});

const sidebar = useSidebarStore();
</script>

<style scoped>
.sidebar {
    display: block;
    position: absolute;
    left: 0;
    top: 70px;
    bottom: 0;
    overflow-y: scroll;
}

.sidebar::-webkit-scrollbar {
    width: 0;
}

.sidebar-el-menu:not(.el-menu--collapse) {
    width: 250px;
}

.sidebar>ul {
    height: 100%;
}
</style>
